package com.example.mbedtlscurl;

import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Keep;

import java.util.Map;

/**
 * author : 徐亚彬
 * e-mail : 724341610@qq.com
 * date   : 2019-08-1513:54
 * desc   :
 * version: 1.0
 */
public class JavaToNative {

    private static final String TAG = JavaToNative.class.getName();

    private OnResponsetListener mListener;

    static {
        System.loadLibrary("native-lib");
    }

    public static abstract class OnResponsetListener {

        /**
         * 网络返回
         */
        public abstract void onResponse(String result);

        /**
         * 出错返回
         */
        public abstract void onFailure(String error);

        /**
         * 超时
         */
        public abstract void onNetTimeOut();
        /**
         * 网络连接有问题
         */
        public abstract  void onNetworkError();
    }

    private native synchronized void CpostAsync(String url, String msg_, String UserAgent, boolean isSingle);

    private native synchronized String Cpost(String url, String msg_, String UserAgent, boolean isSingle);

    private native synchronized void CgetAsync(String url, String useragent);

    private native synchronized String Cget(String url, String useragent);

    public synchronized void post(final String url, final Map<String, String> params, final String UserAgent , final OnResponsetListener listener){

        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder stringBuilder = new StringBuilder();
                if (params!=null && !params.keySet().isEmpty()){
                    for (String key : params.keySet()) {
                        stringBuilder.append(key).append("=").append(params.get(key)).append("&");
                    }
                    stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
                }
                String string = Cpost(url, stringBuilder.toString(), UserAgent,false);
                if (listener!=null) {
                    if (!TextUtils.isEmpty(string)) {
                        String[] strings = string.split("=");
                        int code = Integer.parseInt(strings[0]);
                        String result = string.substring(strings[0].length() + 1);
                        if (code == 0) {
                            listener.onResponse(result);
                        } else {
                            listener.onFailure(result);
                        }
                    } else {
                        listener.onNetworkError();
                    }
                }
            }
        }).start();
    }

    public synchronized void postAsync(final String url, final Map<String, String> params, final String UserAgent, final OnResponsetListener listener){

        mListener = listener;

        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder stringBuilder = new StringBuilder();
                if (params!=null && !params.keySet().isEmpty()){
                    for (String key : params.keySet()) {
                        stringBuilder.append(key).append("=").append(params.get(key)).append("&");
                    }
                    stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
                }
                CpostAsync(url, stringBuilder.toString(), UserAgent,false);
            }
        }).start();
    }

    public synchronized void post(final String url, final String data, final String UserAgent, final OnResponsetListener listener){

        new Thread(new Runnable() {
            @Override
            public void run() {

                String string = Cpost(url, data, UserAgent,true);
                if (listener!=null) {
                    if (!TextUtils.isEmpty(string)) {
                        String[] strings = string.split("=");
                        int code = Integer.parseInt(strings[0]);
                        String result = string.substring(strings[0].length() + 1);
                        if (code == 0) {
                            listener.onResponse(result);
                        } else {
                            listener.onFailure(result);
                        }
                    } else {
                        listener.onNetworkError();
                    }
                }
            }
        }).start();
    }

    public synchronized void postAsync(final String url, final String string, final String UserAgent, final OnResponsetListener listener){

        mListener = listener;

        new Thread(new Runnable() {
            @Override
            public void run() {
                CpostAsync(url, string, UserAgent,true);
            }
        }).start();
    }

    public synchronized void get(final String url, final Map<String, String> params, final String useragent, final OnResponsetListener listener){

        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder stringBuilder = new StringBuilder();
                if (params!=null && !params.keySet().isEmpty()){
                    for (String key : params.keySet()) {
                        stringBuilder.append(key).append("=").append(params.get(key)).append("&");
                    }
                    stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
                }
                String string = Cget(url+"?"+stringBuilder.toString(),useragent);

                if (listener!=null) {
                    if (!TextUtils.isEmpty(string)) {
                        String[] strings = string.split("=");
                        int code = Integer.parseInt(strings[0]);
                        String result = string.substring(strings[0].length() + 1);
                        if (code == 0) {
                            listener.onResponse(result);
                        } else {
                            listener.onFailure(result);
                        }
                    } else {
                        listener.onNetworkError();
                    }
                }
            }
        }).start();
    }

    public synchronized void getAsync(final String url, final Map<String, String> params, final String useragent, final OnResponsetListener listener){

        mListener = listener;

        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder stringBuilder = new StringBuilder();
                if (params!=null && !params.keySet().isEmpty()){
                    for (String key : params.keySet()) {
                        stringBuilder.append(key).append("=").append(params.get(key)).append("&");
                    }
                    stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
                }
                CgetAsync(url+"?"+stringBuilder.toString(),useragent);
            }
        }).start();
    }

    /*post请求返回*/
    @Keep
    public void postCallBack(int status, String string){
        Log.d(TAG,"-------->>>>>>>>>"+string);
        if (mListener!=null){
            if (status==0){
                mListener.onResponse(string);
            }else {
                mListener.onFailure(string);
            }
        }
    }

    /*get请求返回*/
    @Keep
    public void getCallBack(int status, String string){
        Log.d(TAG,"-------->>>>>>>>>"+string);
        if (mListener!=null){
            if (status==0){
                mListener.onResponse(string);
            }else {
                mListener.onFailure(string);
            }
        }
    }

}
